﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;

namespace CosmosKey.WindowsUtils.Downr
{
    public partial class DownrForm : Form
    {
        public DownrForm()
        {
            InitializeComponent();
            SetUIValues();
            System.Threading.Thread.Sleep(1000);           
        }

        private void SetUIValues()
        {
            this.Text = Config.TitleString;
            ShutdownButton.Text = Config.ShuttingDownButtonString;
            PostponeButton.Text = Config.PostponeButtonString;
            CountDownLabel.Text = string.Format(Config.CountingString, Config.Counter);
        }
        private void CountDownTimer_Tick(object sender, EventArgs e)
        {
            if (Config.Counter < 1)
            {
                CountDownLabel.Text = Config.ShuttingDownNowString;
                Shutdown();                
            }
            else
            {
                CountDownLabel.Text = string.Format(Config.CountingString, --Config.Counter);
            }
        }


        void Shutdown()
        {
            //Application.Exit();
            ManagementBaseObject mboShutdown = null;
            ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
            mcWin32.Get();

            // You can't shutdown without security privileges
            mcWin32.Scope.Options.EnablePrivileges = true;
            ManagementBaseObject mboShutdownParams =
                     mcWin32.GetMethodParameters("Win32Shutdown");

            // Flag 1 means we want to shut down the system. Use "2" to reboot.
            /*
             *  0 (0x0)
             *  Log Off
             *          
             *  4 (0x4)
             *  Forced Log Off (0 + 4)
             *  
             *  1 (0x1)
             *  Shutdown
             * 
             *  5 (0x5)
             *  Forced Shutdown (1 + 4)
             *  
             *  2 (0x2)
             *  Reboot
             *  
             *  6 (0x6)
             *  Forced Reboot (2 + 4)
             *  
             *  8 (0x8)
             *  Power Off
             *   
             *  12 (0xC)
             *  Forced Power Off (8 + 4)
             *  
             */
        
            mboShutdownParams["Flags"] = "5";
            mboShutdownParams["Reserved"] = "0";
            foreach (ManagementObject manObj in mcWin32.GetInstances())
            {
                mboShutdown = manObj.InvokeMethod("Win32Shutdown", 
                                               mboShutdownParams, null);
            }
        }

        private void ShutdownButton_Click(object sender, EventArgs e)
        {
            CountDownLabel.Text = Config.ShuttingDownNowString;
            Shutdown();
        }

        public void StartCountingDown()
        {
            CountDownTimer.Start();
            Config.Counter = Config.CounterSeconds;
        }

        private void PostponeButton_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            CountDownTimer.Stop();
            Config.TimeToStart = DateTime.Now.TimeOfDay.Add(new TimeSpan(0,Config.PostponeTime,0));
        }
    }
}
